//
//  TELocalizableTool.m
//  TELM
//
//  Created by xuliang on 16/1/13.
//  Copyright © 2016年 Tough Egg Pte Ltd. All rights reserved.
//

#import "TEALocalizableTool.h"

@interface TEALocalizableTool ()

#define LanguageList @[@"en",@"de",@"fr",@"it",@"nl",@"cs-CZ",@"pl-PL",@"da",@"sv-AX",@"nb",@"fi",@"is",@"lv",@"es",@"zh-Hans",@"en"]
#define LanguageRangeList @[@"en",@"de",@"fr",@"it",@"nl",@"cs",@"pl",@"da",@"sv",@"nb",@"fi",@"is",@"lv",@"es",@"zh-Hans-CN",@"en-CN"]

@property (nonatomic, strong) NSBundle *currentBundle;

@end

@implementation TEALocalizableTool

+ (instancetype)shareInstance {
    static TEALocalizableTool *localizableTool = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        localizableTool = [[TEALocalizableTool alloc] init];
    });
    return localizableTool;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initLanguage];
    }
    return self;
}

- (void)initLanguage {
    NSString *saveLan = [[NSUserDefaults standardUserDefaults] objectForKey:UserLanguage];
    NSString *newLan = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] firstObject];
    if (saveLan == nil || (![newLan isEqualToString:saveLan])) {
        saveLan = nil;
        BOOL isSearch = NO;
        for (int i = 0; i < 16; i++) {
            NSString *string = LanguageRangeList[i];
            if ([newLan rangeOfString:string].location != NSNotFound) {
                saveLan = LanguageList[i];
                isSearch = YES;
                break;
            }
        }
        
        if (isSearch == NO) {
            saveLan = @"en";
        }
//        if ([saveLan rangeOfString:@"zh-Hans"].location != NSNotFound) {    //中文
//            saveLan = @"zh-Hans";
//        }else if ([saveLan rangeOfString:@"zh-Hant"].location != NSNotFound || [saveLan rangeOfString:@"zh-HK"].location != NSNotFound ) {    //中文繁体
//            saveLan = @"zh-Hant";
//        }else if ([saveLan rangeOfString:@"en"].location != NSNotFound) {         //英文
//            saveLan = @"en";
//        }else if ([saveLan rangeOfString:@"ko"].location != NSNotFound) {         //韩文
//            saveLan = @"ko";
//        }else{
//            saveLan = @"en";
//        }
    }
    NSString *path = [[NSBundle mainBundle] pathForResource:saveLan ofType:@"lproj"];
    NSBundle *bundle = [NSBundle bundleWithPath:path];
    if (bundle) {
        _currentLanguage = saveLan;
        _currentBundle = bundle;
    }
    [[NSUserDefaults standardUserDefaults] setObject:_currentLanguage forKey:UserLanguage];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@synthesize currentLanguage = _currentLanguage;
- (void)setCurrentLanguage:(NSString *)currentLanguage {
    if (_currentLanguage == currentLanguage) {
        return;
    }
    NSString *path = [[NSBundle mainBundle] pathForResource:currentLanguage ofType:@"lproj"];
    NSBundle *bundle = [NSBundle bundleWithPath:path];
    if (bundle) {
        _currentLanguage = currentLanguage;
        _currentBundle = bundle;
    }
    [[NSUserDefaults standardUserDefaults] setObject:_currentLanguage forKey:UserLanguage];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSString *)currentLanguage {
    return _currentLanguage;
}

- (NSString *)localizedStringForKey:(NSString *)key {
    NSString *value = [_currentBundle localizedStringForKey:key value:nil table:nil];
    if (value) {
        return value;
    }else {
        return key;
    }
}

@end
